package com.probiz.estore.exports.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.product.ProductSkuOptionValue;
import com.probiz.estore.common.model.product.WholesalePrice;
import com.probiz.estore.core.util.ExcelUtils;
import com.probiz.estore.exports.handler.ColumnHandler;
import com.probiz.estore.exports.model.Column;
import com.probiz.estore.exports.service.ProductExportManager;
import com.probiz.estore.product.service.ProductManager;

public class ProductExportManagerImpl extends BaseExportManagerImpl implements ProductExportManager {
	private ProductManager productManager;
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	
	@Override
	public List<Object> getDataSource(String[] ids) {
		List list = new ArrayList();
		if(ids != null && ids.length>0){
			for (String id : ids) {
				Object o = this.productManager.loadById(Integer.valueOf(id));
				list.add(o);
			}
		} else {
			list = this.productManager.getListByDelete();
		}
		return list;
	}

	
	@Override
	public boolean ifExistChildren(Object target){
		Product product = (Product)target;
		return product.getProductSkus().size()>1;
	}
	@Override
	public Integer buildExcelChildren(Object target, HSSFSheet sheet,
			List<Column> columns, List<String> listHeader, Integer rowIdx){
		Product product = (Product)target;
		Set<ProductSku> set_sku = product.getProductSkus();
		Iterator<ProductSku> it = set_sku.iterator();
		Integer num = 0;
		while(it.hasNext()){
			HSSFRow row = sheet.createRow(rowIdx);
			ProductSku sku = it.next();
			if(num == 0) {
				for (Column column : columns) {
					ColumnHandler handler = column.getHandler();
					String attrValue = handler.getProperty(target, column, sheet, row, listHeader);
					int cell = columns.indexOf(column);
					if(StringUtils.isNotBlank(attrValue)){
						ExcelUtils.setCellStringValue(row,(short)cell,attrValue);
					}
				}
			} else {
				/*商品编码   * 货号  * 商品名称 * 售价 * 特价 * 市场价 * 重量 * 库存	 * 批发价 * 规格选项*/
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("商品编码"), product.getProductCode());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("货号"), sku.getProductSkuCode());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("商品名称"), "");
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("售价"), sku.getPrice()==null?"":sku.getPrice().toString());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("特价"), sku.getSalePrice()==null?"":sku.getSalePrice().toString());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("市场价"), sku.getListPrice()==null?"":sku.getListPrice().toString());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("重量"), sku.getWeight()==null?"":sku.getWeight().toString());
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("库存"), sku.getInventory().getQuantityOnHand()==null?"":sku.getInventory().getQuantityOnHand().toString());
				//批发价
				Set<WholesalePrice> set_wp = sku.getWholesalePrices();
				Iterator<WholesalePrice> it_wp = set_wp.iterator();
				StringBuffer wp_str = new StringBuffer("");;
				while(it_wp.hasNext()){
					WholesalePrice wp = it_wp.next();
					//2-20=121;21-40=118;
					wp_str.append(wp.getMinQuantity()).append("-").append(wp.getMaxQuantity()).append("=").append(wp.getPrice()).append(";");
				}
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("批发价"), wp_str.toString());
				//规格选项
				Set<ProductSkuOptionValue> set_psov =  sku.getProductSkuOptionValues();
				Iterator<ProductSkuOptionValue> it_psov = set_psov.iterator();
				StringBuffer psov_str = new StringBuffer("");;
				while(it_psov.hasNext()){
					ProductSkuOptionValue psov = it_psov.next();
					//尺码=XL;颜色=青
					psov_str.append(psov.getSkuOptionValue().getSkuOption().getSkuOptionName()).append("=").append(psov.getSkuOptionValue().getSkuOptionValueName()).append(";");
				}
				ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("规格选项"), psov_str.toString());
			}
			num++;
			rowIdx++ ;
		}
		return rowIdx;
	} 
}
